<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org"  lang="en"
      xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <meta charset="UTF-8" />
    <title>xxx</title>
    <script src="/plugin/jquery/jquery-1.9.0.min.js" type="text/javascript"></script>
    <link href="/plugin/ligerUI/skins/Aqua/css/ligerui-all.css" rel="stylesheet" type="text/css" />

    <script src="/plugin/ligerUI/js/ligerui.all.js" type="text/javascript"></script>

    <link href="/plugin/ligerUI/skins/ligerui-icons.css" rel="stylesheet" type="text/css" />
    <link href="/plugin/ligerUI/skins/Gray/css/all.css" rel="stylesheet" type="text/css" />
    <script src="/plugin/ligerUI/js/core/init-obj.js" type="text/javascript"></script>

    <script src="/js/mask.js" type="text/javascript"></script>

    <style type="text/css">
        /* 半透明的遮罩层 */
        #overlay {
            background: #000;
            filter: alpha(opacity=50); /* IE的透明度 */
            opacity: 0.5;  /* 透明度 */
            display: none;
            position: absolute;
            top: 0px;
            left: 0px;
            width: 100%;
            height: 100%;
            z-index: 50; /* 此处的图层要大于页面 */
            display:none;
        }
        #dig{ height: 300px; display: none}
    </style>
    <script th:inline="javascript">

        var manager = null;
        var navtab = null;
        var tree8=null;
        var menus =null;
        var orgIds = '';
        $(function ()
        {


            //页面布局
            $("#layout1").ligerLayout({ leftWidth: 150});
            $("#tab1").ligerTab();
            navtab = $("#tab1").ligerGetTabManager();

            //组织树
            tree8 =   $("#tree8").ligerTree({
                url:'/sec/org/tree',
                ajaxType:'post',
                checkbox: false,
                parentIcon: 'folder',
                childIcon: 'leaf' ,
                slide: false, //是否显示动画
                nodeWidth: 100,
                idFieldName: 'id',
                textFieldName:'name' ,
                onSelect: function (node)
                {
                    navtab.removeAll();
                    $("#orgIdF").val(node.data.id);
                     orgIds = '';
                   var childs = getChild(node);
                   console.log(childs)
                    $("#orgId").val(childs)
                    doSearch();

                },
                onError:function(node){
                    $.ligerDialog.error('组织列表加载失败!');
                }
            });

            doSearch();
        });

        //查看用户权限
        function menuTree(secId){
            showDigWin("权限","dig",secId);

        }

        //显示用户角色
        function showRoles(title,id){
            showOverlay();
            var dlgedit=$.ligerDialog.open({
                title:title,
                url:'/sec/user/loadRole?id='+id,
                width:850,
                height:450,
                buttons: [
                    { text: '保存', onclick: function (i, d) {
                        $("input").ligerHideTip(); d.hide();
                        hideOverlay();
                }},
                    { text: '关闭', onclick: function (i, d) {
                        $("input").ligerHideTip(); d.hide();
                        hideOverlay();
                    }}
                ]
            });
            $(".l-dialog-close").bind('mousedown',function()  //dialog右上角的叉
            {
                $("input").ligerHideTip();
                dlgedit.hide();
                hideOverlay();
            });

        }

        //保存权限
        function save(){
            var ids = '';
            var data =menus.getChecked();
            for(var i=0 ; i<data.length ; i++){
                if(data[i].data.flag!='0')
                    ids+=data[i].data.id+',';
            }

            var obj = new Object();
            obj.menuId=ids;
            obj.userId=$("#secId").val();
            $.ajax({
                type: 'POST',
                url: '/sec/menu/member/bacthAdd',
                data: obj,
                dataType: 'json',
                success: function (data) {
                    if(data.code==200){
                        $.ligerDialog.success('保存成功');
                    }else{
                        $.ligerDialog.error('删除失败');
                    }
                },
                error:function(){
                    $.ligerDialog.error('删除失败');
                }
            });


        }

        //打开菜单树弹窗
        function showDigWin(title,id,secId){

              showOverlay();
            var dlgedit=$.ligerDialog.open({
                title:title,
                target:$("#"+id),
                buttons: [  { text: '保存', onclick: function (i, d) {
                    save();
                }},
                    { text: '关闭', onclick: function (i, d) {
                        $("input").ligerHideTip(); d.hide();
                        hideOverlay();
                        menus.reload();
                    }}
                ]
            });
            $(".l-dialog-close").bind('mousedown',function()  //dialog右上角的叉
            {
                $("input").ligerHideTip();
                dlgedit.hide();
                hideOverlay();
            });

            $("#secId").val(secId);
            //菜单树
            menus= $("#tree9").ligerTree({
                url:'/sec/user/acthList',
                parms:{
                    id:secId
                },
                ajaxType:'post',
                checkbox: true,
                parentIcon: 'folder',
                childIcon: 'leaf' ,
                slide: false, //是否显示动画
                nodeWidth: 100,
                autoCheckboxEven:true,
                idFieldName: 'id',
                textFieldName:'name' ,
                parentIDFieldName:'parentId' ,
                onSuccess:function(data){
                    for(var i=0;i<data.length;i++){
                        if(data[i].flag=='0'){
                            f_selectNode(data[i].id);
                            $("#"+data[i].id).find('.l-body .l-checkbox').unbind().click(function(){
                                return false;
                            });
                        }else if(data[i].flag=='1'){
                            f_selectNode(data[i].id);
                        }
                    }

                }
            });
        }

        //勾选树节点
        function f_selectNode(str) {
            str = "," + str+",";
            var parm = function(data) {
                if (str.indexOf("," + data.id + ",") != -1)
                { return true; } else { return false }
            };
            menus.selectNode(parm);
        };

        var selectId = '';
        //获取权限树父父节点
        function getChildId(id){
           var data = menus.getDataByID(id);

            if(data!=null){
                   selectId+=data.id+",";

                   if(data.parentId!='0'){
                       var p = menus.getDataByID(data.parentId);
                       selectId+=p.id+",";
                       if(p!=null){
                           getChildId(p.parentId);
                       }
                   }
            }
        }
        var orgTree = '';
        //获取组织树父父节点
        function getOrgChildId(id){
            var data = tree8.getDataByID(id);
            if(data!=null){
                orgTree+=data.id+",";

                if(data.parentId!='0'){
                    var p = tree8.getDataByID(data.parentId);
                    orgTree+=p.id+",";
                    if(p!=null){
                        getOrgChildId(p.parentId);
                    }
                }
            }
        }

        function getChildren(id){
            orgTree='';
            getOrgChildId(id);
            return orgTree;
        }


        //获取组织子节点，查询
        function getChild(node){

            orgIds+=node.data.id+",";
            if(node.data.children!=null){
                var arr = node.data.children;
                for(var i=0;i<arr.length;i++){
                    orgIds+=arr[i].id+",";
                }
            }
            return orgIds;
        }

        var height=null;

        //工具条按钮事件
        function itemclick(item)
        {
             height = $(".l-layout-center").height();
            if(item=='update'){

                var obj = dataGrid.getSelected();
                if(obj!=null)
                    f_addTab1(item,'修改用户','linkUpdate?id='+obj.id,navtab,height);
                else
                    $.ligerDialog.question('请选择一行数据后编辑');
            }
            else if(item=='add'){
                if($("#orgIdF").val()!='')
                f_addTab1(item,'添加用户','linkAdd',navtab,height);
                else
                    $.ligerDialog.question('请选择组织节点');
            }
            else {
                var obj= dataGrid.getSelecteds();
                if(obj.length>0){
                    var ids = '';
                    /*<![CDATA[*/
                    for (var i = 0; i < obj.length; i++) {
                        ids += obj[i].id + ",";
                    }
                    deletedRows(ids);
                }
                else{
                    $.ligerDialog.question('请选择要删除的数据');
                }
            }
        }

        //子页面回调函数
        function cellValOrgId(){
            return $("#orgIdF").val();
        }

        //删除
        function deletedRows(ids){
            $.ligerDialog.confirm('确定删除吗?', function (yes){
                if(yes) {
                    $.ajax({
                        type: 'POST',
                        url: 'deleteBacth',
                        data: {
                            ids: ids
                        },
                        dataType: 'json',
                        success: function (data) {
                            if(data.code==200){
                                dataGrid.reload();
                            }else{
                                $.ligerDialog.error('删除失败');
                            }
                        },
                        error:function(){
                            $.ligerDialog.error('删除失败');
                        }
                    });
                }
            });
        }


        function callBack()
        {
            navtab.removeTabItem('add');
            navtab.removeTabItem('update');
            dataGrid.reload();
            $.ligerDialog.success('保存成功');
        }

        var dataGrid =null;
        //加载列表
        function loadDataAptitude(data){
            dataGrid   =$("#maingrid").ligerGrid({
                checkbox:true,
                height: '100%',
                columns: [
                    { display: '操作', value: '操作', minWidth: 120,render: function (rowdata)
                        {
                            // ''
                            return '<a onclick=menuTree("'+rowdata.id+'") href=javasrcipt:;>权限</a>|' +
                                '<a onclick=openT("'+rowdata.id+'"); href=javasrcipt:;>角色</a>';
                        }
                    },
                    { display: '用户名', name: 'userName', minWidth: 120 },
                    { display: '性别', name: 'sex' ,minWidth: 140,render: function (rowdata)
                        {
                            if (rowdata.sex == 0)
                            {
                                return "女";
                            }
                                return "男";
                         }
                    },
                    { display: '账号', name: 'account',minWidth: 180 },
                    { display: '手机号码', name: 'mobile', minWidth: 140 },
                    { display: '邮箱', name: 'email' ,minWidth: 140},
                    { display: '地址', name: 'addr' ,minWidth: 140},
                    { display: '状态', name: 'status' ,minWidth: 140,render: function (rowdata)
                    {
                        if (rowdata.status == 0)
                        {
                            return "不可用";
                        }
                        return "可用";
                    }},
                    { display: '创建人', name: 'creator' ,minWidth: 140},

                    { display: '创建时间', name: 'createTime', minWidth: 140 ,type:'date'}
                ],
                parms:data,
                rownumbers: true,
                autoFilter: true,
                dataAction:"local",
                url:"list",
                usePager:true,
                pageSize:"20",//分页页面大小
                pageSizeOptions:[10,20,30]//可指定每页页面大小
            });
            var s=  $(".l-panel-topbarinner .l-toolbar-item:last").find(".l-icon-search2");
            if(s.length==1){
                $(".l-panel-topbarinner .l-toolbar-item:last").remove();
            }
        }



        var tabs =null
        function doSearch(){
            var data = $("#searchFrom").serializeArray();
            loadDataAptitude(data);
        }

        function doReset(){
            $("#searchFrom")[0].reset();
        }

        function openT(id){
            showRoles("查看角色",id);
        }


    </script>
</head>
<body style="padding:4px">
<div id="layout1">
    <input type="hidden" id="secId" />
    <div  position="left" title="组织树">
        <ul id="tree8"></ul>
    </div>
    <div  position="center" title="">
        <input type="hidden"  id="orgIdF" value=""/>
        <form id="searchFrom">
            <input type="hidden" name="orgId" id="orgId" />
        </form>
        <div id="tab1" style="width: 100%;overflow:hidden;">
            <div id="tis" title="用户列表">
                <div>
                    <a shiro:hasPermission="/sec/user/linkAdd"  href="javascript:itemclick('add')">增加</a>
                    <a shiro:hasPermission="/sec/user/delete"  href="javascript:itemclick('delete')">删除</a>
                    <a shiro:hasPermission="/sec/user/linkUpdate"  href="javascript:itemclick('update')">修改</a>
                </div>
                <div class="l-clear"></div>
                <div  id="maingrid"></div>
            </div>
        </div>
    </div>
</div>
<div id="dig">
    <ul id="tree9"></ul>
</div>

<div id="overlay"></div>

</body>
</html>